<template>
  <div>
    <p>
      js中的class<br />
      class写法就是语法糖，把方法定义在原型链上<br />
    </p>
    <el-button type="primary" @click="learnClass">learnClass</el-button>
    <el-divider></el-divider>

    <p>
      静态方法<br />
      js的静态方法放在constructor下
    </p>
    <el-button type="primary" @click="learnStaticMethod">learnStaticMethod</el-button>
    <el-divider></el-divider>

    <p>
      访问器<br />
    </p>
    <el-button type="primary" @click="learnGetterAndSetter">learnGetterAndSetter</el-button>
    <el-divider></el-divider>
  </div>
</template>

<script>
export default {
  methods: {
    learnClass () {
      class User {
        show () { }
        get () {
          console.log("get method");
        }
      }
      const hd = new User();
      hd.get();
      console.log(hd)

      // class写法就是语法糖，把方法定义在原型链上
      function Person () {
      }
      Person.prototype.show = function () { };
      Person.prototype.get = function () {
        console.log("get method");
      }

      let test = new Person();
      console.log(test)
    },
    learnStaticMethod () {
      function User () {
        this.show = function () {
          return "this is a object function";
        };
      }
      User.show = function () {
        return "welcome to houdunren";
      };
      const xj = new User();
      console.log(xj)
      console.log(xj.__proto__)
      console.log(User)
      //   console.dir(xj.show()); //this is a object function
      //   console.dir(User.show()); //welcome to houdunren
    },
    learnGetterAndSetter () {
      class User {
        constructor(name) {
          this.data = { name };
        }
        get name () {
          return this.data.name;
        }
        set name (value) {
          if (value.trim() == "") throw new Error("invalid params");
          this.data.name = value;
        }
      }
      let hd = new User("向军大叔");
      hd.name = "后盾人";
      console.log(hd.name);

    }
  }
}
</script>

<style lang="less" scoped>
p {
  line-height: 30px;
}
</style>